<?php
// +----------------------------------------------------------------------
// | ranking [ 捐款平台公款系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://localhost.ranking.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 七秒の记忆 <Majw998@Gmail.com>
// +----------------------------------------------------------------------
namespace Home\Model;
use Think\Model;

class FileModel extends Model{
	protected $_validate = array(
		array('name', 'require', '文件名不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
	);

	protected $_auto = array(
		array('create_time', NOW_TIME, self::MODEL_INSERT),
	);


	public function update($data){
		$info       = array();
		$info['e']  = 1;

		if(!$this->create($data)) {
			$info['e']    = -1;
			$info['info'] = $this->getError();
			return $info;
		}

		if($data['id']) {
			$info['info'] = $this->save();
		} else {
			$info['info'] = $this->add();
		}
		return $info;
	}


	public function singleUpload($file){
		$upload = new \Think\Upload();
	    $upload->maxSize    =     C('UPLOAD_CONFIG.MAXSIZE') ;// 设置附件上传大小
	    $upload->exts       =     C('UPLOAD_CONFIG.EXTS');// 设置附件上传类型
	    $upload->rootPath   =     C('UPLOAD_CONFIG.ROOTPATH'); // 设置附件上传根目录

	    $info   =   $upload->uploadOne($file);
	    $data['name'] 		= $info['name'];
	    $data['savename']	= $info['savename'];
	    $data['savepath']   = C('UPLOAD_CONFIG.SAVEPATH') . $info['savepath'];
	    $picid 				= $this->update($data);

	    $picinfo['path'] = $data['savepath'] . $info['savename'];
	    $picinfo['id']   = $picid['info'] ;
	    $picinfo['e']    = $picid['e'] ;

	    return $picinfo;
	}


}